package com.lsl.test;

import cn.hutool.core.util.ClassLoaderUtil;
import org.junit.Test;
import org.junit.experimental.theories.suppliers.TestedOn;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author lishuailong
 * @create 2020-07-14 16:19
 */
public class test1 {
    @Test
    public void helloReflect() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
        String s="123";
        System.out.println(s.getClass().getSimpleName());
        System.out.println(s.getClass());

        Class<String> classz = String.class;
        Constructor<String> constructor = classz.getConstructor(String.class);
        String s1 = constructor.newInstance("李帅龙");
        System.out.println("s1 = " + s1);

        Method[] methods = classz.getMethods();
        for (Method method : methods) {
            System.out.println(method.getName());
        }
    }
    @Test
    public void helloReflect1() throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class<Person> personClass = Person.class;

        Person person = new Person("李帅龙",20);
        System.out.println(person);

        Field age = personClass.getDeclaredField("age");
        age.setAccessible(true);
        age.set(person,23);
        System.out.println(person);

        Method introduce = personClass.getDeclaredMethod("Introduce");
        Object invoke = introduce.invoke(person);
        System.out.println(invoke);


    }

    /**获得Class对象 4种方法
     * @throws NoSuchFieldException
     * @throws IllegalAccessException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     */
    @Test
    public void helloReflect2() throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException {
        //方式一
        Class<Person> classz1 = Person.class;
        System.out.println(classz1.hashCode());

        //方式二
        Person person = new Person();
        Class<Person> classz2 = (Class<Person>) person.getClass();
        System.out.println(classz2.hashCode());

        //方式三
        Class classz3 = Class.forName("com.lsl.test.Person");
        System.out.println(classz3.hashCode());

        //方式四(类加载器)
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        Class<?> classz4 = classLoader.loadClass("com.lsl.test.Person");
        System.out.println(classz4.hashCode());

        Class<?> aClass = ClassLoaderUtil.loadClass("com.lsl.test.Person");

    }
    @Test
    public void helloReflect3() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Class<Person> aClass = (Class<Person>)Class.forName("com.lsl.test.Person");

        //方式一
        Person person = aClass.newInstance();
        System.out.println(person);
        //方式二
        Constructor<Person> personConstructor = aClass.getDeclaredConstructor(String.class,Integer.class);
        Person person1 = personConstructor.newInstance("李帅龙", 20);
        System.out.println(person1);

    }
    @Test
    public void helloReflect4() throws IllegalAccessException, InstantiationException, ClassNotFoundException {
        Person person = (Person)getInstantiation("com.lsl.test.Person");
        System.out.println(person);


    }
    public Object getInstantiation(String classPath) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        Class<?> aClass = Class.forName(classPath);
        return aClass.newInstance();
    }
}















